import config from "@/config";
import { UserAuthInfo } from "@/models/user";

/**
 * 存储sessionStorage
 */
export const setStore = (name: string, content: string | object) => {
  if (!name) return;
  if (typeof content !== "string") {
    content = JSON.stringify(content);
  }
  window.sessionStorage.setItem(`${config.projectName}-${name}`, content);
};

/**
 * 获取sessionStorage
 */
export const getStore = (name: string) => {
  if (!name) return;
  return window.sessionStorage.getItem(`${config.projectName}-${name}`);
};

/**
 * 删除sessionStorage
 */
export const removeStore = (name: string) => {
  if (!name) return;
  window.sessionStorage.removeItem(`${config.projectName}-${name}`);
};

/**
 * 存储localStorage
 */
export const setLocalStore = (name: string, content: string | object) => {
  if (!name) return;
  if (typeof content !== "string") {
    content = JSON.stringify(content);
  }
  window.localStorage.setItem(`${config.projectName}-${name}`, content);
};

/**
 * 获取localStorage
 */
export const getLocalStore = (name: string) => {
  if (!name) return;
  return window.localStorage.getItem(`${config.projectName}-${name}`);
};

/**
 * 删除localStorage
 */
export const removeLocalStore = (name: string) => {
  if (!name) return;
  window.localStorage.removeItem(`${config.projectName}-${name}`);
};

export const setLoginAuthLocalStore = (data: UserAuthInfo) => {
  // 设置保存登录鉴权信息
  setLocalStore("LoginAuthInfo", data);
};

export const getLoginAuthLocalStore = () => {
  // 获取保存登录鉴权信息
  const stringData = getLocalStore("LoginAuthInfo");
  if (stringData) {
    const result: UserAuthInfo = JSON.parse(stringData);
    return result;
  } else return null;
};

export const removeLoginAuthLocalStore = () => {
  // 删除保存登录鉴权信息
  removeLocalStore("LoginAuthInfo");
};
